﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace _2_14
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnCalculate_Click(object sender, EventArgs e)
        {
            try
            {
                int n = int.Parse(txtN.Text);
                int s = 1;

                for (int i = 1; i <= n; i++)
                {
                    checked
                    {
                        s *= i; // 使用checked块检查溢出
                    }
                }

                txtResult.Text = s.ToString();
            }
            catch (FormatException)
            {
                MessageBox.Show("输入格式不合法！");
                txtN.Text = "";
                txtN.Focus();
            }
            catch (OverflowException)
            {
                MessageBox.Show("溢出错误！");
                txtN.Text = "";
                txtN.Focus();
            }
            catch (Exception ex)
            {
                MessageBox.Show("其它异常！");
                txtN.Text = "";
                txtN.Focus();
            }
        }

        private void tnClear_Click(object sender, EventArgs e)
        {
            txtN.Text = "";
            txtResult.Text = "";
            txtN.Focus();
        }
    }
}
